package soap.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import soap.commons.ReturnCommons;
import soap.entity.po.ExportRecord;
import soap.entity.po.User;
import soap.feign.query.ExportRecordQuery;
import soap.manager.ExportRecordManager;
import soap.service.ExportRecordService;
import soap.utils.AuthUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by ZhangPY on 2020/6/7
 * Belong Organization OVERUN-9299
 * overun9299@163.com
 * Explain:
 */
@Service
public class ExportRecordServiceImpl implements ExportRecordService {

    @Autowired
    private ExportRecordManager exportRecordManager;

    @Override
    public void saveExportRecord(ExportRecord exportRecord) {
        exportRecordManager.save(exportRecord);
    }

    @Override
    public void updateExportRecord(ExportRecord exportRecord) {
        exportRecordManager.updateById(exportRecord);
    }


    @Override
    public String getExportRecordListByPage(HttpServletRequest request, ExportRecordQuery exportRecordQuery) {
        User userFromRequest = AuthUtils.getUserFromRequest(request);
        Page<ExportRecord> page = new Page<>(exportRecordQuery.getPage(), exportRecordQuery.getLimit());
        QueryWrapper<ExportRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("requestor_id", userFromRequest.getId());
        wrapper.orderByDesc("request_date");
        IPage<ExportRecord> iPage = exportRecordManager.page(page, wrapper);
        return ReturnCommons.getSuccess(iPage);
    }
}
